/**
 *
 * Copyright (C) 2008  Igor Vdovichenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Though a sincere effort has been made to deliver a professional,
 * quality product,the library itself is distributed WITHOUT ANY WARRANTY;
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 */
package com.vin.scriptutils.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;

/**
 *
 * @author igor
 */
public class Nodes {

    private Collection<Node> cmndNodes = new LinkedList<Node>();
    private final QuotingStrategy startegy;

    public Nodes() {
        this.startegy = new BlankQuotingStrategy();
    }

    public Nodes(QuotingStrategy startegy) {
        this.startegy = startegy;
    }

    public Collection<Node> getNodes() {
        return this.cmndNodes;
    }

    public void node(Node node) {
        if (node != null) {
            this.cmndNodes.add(node);
        }
        return;
    }

    public void node(String node) {
        if (node == null) {
            return;
        }
        node(new Node(node));
    }

    public void nodes(Node... nodes) {
        if (nodes == null) {
            return;
        }
        nodes(Arrays.asList(nodes), false);
    }

    public void nodes(String... nodes) {
        if (nodes == null) {
            return;
        }
        nodes(Arrays.asList(nodes), false);
    }

    public void nodesEsc(String... nodes) {
        if (nodes == null) {
            return;
        }
        nodes(Arrays.asList(nodes), true);
    }

    private void nodes(Iterable<? extends Object> nodes, boolean esc) {
        if (nodes == null) {
            return;
        }
        for (Object o : nodes) {
            if (o instanceof Node) {
                node((Node) o);
            } else {
                String n = this.startegy.getQuoted(o.toString());
                if (esc) {
                    node(new EscapedString(n).getString());
                } else {
                    node(n);
                }
            }
        }
        return;
    }
}
